<?php
class FunduserController  extends AdminBaseController{
	public function lists (){
		$user = HttpServerManager::getFunduserServer();
		$data = $user->pagingBySearch($_GET);
		
		$fund_user_status = Common::_t('fund_user_status');
		$fund_user_rank = Common::_t('fund_user_rank');
		foreach ($data['list'] as $k=>$v){
			$data['list'][$k]['fund_status_name'] = $fund_user_status[$v['status']];
			$data['list'][$k]['fund_user_rank_name'] = $fund_user_rank[$v['fund_user_rank']];
		}
		
		/**
		 页面标签使用
		 */
		$page_fund_status_option = $fund_user_status;
		$data['page_fund_status_option']= $page_fund_status_option;
		$data['page_user_rank_option']= $fund_user_rank;
		
		return $data;
	}
	
	public function userLists(){
		$admin_user_info = $_SESSION['admin'];
		$fund_user_id =0;
		if($admin_user_info['admin_type']==ADMIN_TYPE_FUNDUSER){
			$fund_user_id = $admin_user_info['fund_user_id'];
		}
		
		$id = $_GET['id'];
		if(isset($id)&&$id>0){
			$fund_user_id = $id;
		}
		$where['fund_user_id'] = $fund_user_id;
		$data = HttpServerManager::getUserServer()->pagingBySearch($where);
		$status = Common::_t('confirm_status');
		$user_type = Common::_t('user_type');
		foreach ($data['list'] as $k=>$v){
			$data['list'][$k]['status_name'] = $status[$v['status']];
			$data['list'][$k]['user_type_name'] = $user_type[$v['user_type']];
		}	
		
		$page_status_option = $status;
		$data['page_status_option']= $page_status_option;
		$data['page_user_type_option']= $user_type;		
		
		$level_list = HttpDaoManager::getLevelDao()->searchList();
		$level_pair = array();
		foreach($level_list as $k=>$v){
			if(isset($v['id'])){
				$level_pair[$v['id']]='Level'.'-'.$v['level'];
			}
		}
		unset($level_list);
		$data['level_pair'] = $level_pair;	
			
		return $data;
		
	}


	public function userInviteLists(){
		$admin_user_info = $_SESSION['admin'];
		$fund_user_id =0;
		if($admin_user_info['admin_type']==ADMIN_TYPE_FUNDUSER){
			$fund_user_id = $admin_user_info['fund_user_id'];
		}
		$id = $_GET['id'];
		if(isset($id)&&$id>0){
			$fund_user_id = $id;
		}		
		$where = array();
		$where['fund_user_id'] = $fund_user_id;
		$user = HttpServerManager::getFunduserServer();
		$data = $user->pagingBySearch($where);
		
		$fund_user_status = Common::_t('fund_user_status');
		$fund_user_rank = Common::_t('fund_user_rank');
		foreach ($data['list'] as $k=>$v){
			$data['list'][$k]['fund_status_name'] = $fund_user_status[$v['status']];
			$data['list'][$k]['fund_user_rank_name'] = $fund_user_rank[$v['fund_user_rank']];
		}
		
		/**
		 页面标签使用
		 */
		$page_fund_status_option = $fund_user_status;
		$data['page_fund_status_option']= $page_fund_status_option;
		$data['page_user_rank_option']= $fund_user_rank;
		
		return $data;
	}
	

	public function userFindLists(){
		$admin_user_info = $_SESSION['admin'];
		$fund_user_id =0;
		if($admin_user_info['admin_type']==ADMIN_TYPE_FUNDUSER){
			$fund_user_id = $admin_user_info['fund_user_id'];
		}
		$id = $_GET['id'];
		if(isset($id)&&$id>0){
			$fund_user_id = $id;
		}
				
		$data = array();
		$data = $_GET;
		if(isset($_GET['search'])&&$_GET['search']>0){
			$where = array();
			$where['email'] = trim($_GET['email']);
			$where['phone'] = trim($_GET['phone']);
			$where['qq'] = trim($_GET['qq']);
			if(!empty($where['email'])||!empty($where['phone'])||!empty($where['qq'])){
				$where['unfund_user_id'] = 1;
				$data = HttpServerManager::getUserServer()->pagingBySearch($where);
				$status = Common::_t('confirm_status');
				$user_type = Common::_t('user_type');
				foreach ($data['list'] as $k=>$v){
					$data['list'][$k]['status_name'] = $status[$v['status']];
					$data['list'][$k]['user_type_name'] = $user_type[$v['user_type']];
				}
			}
		}

		$data['level_pair'] = $level_pair;
		$data['id'] = $fund_user_id;
		return $data;
	
	}
	
	public function userLinkLists(){
		$admin_user_info = $_SESSION['admin'];
		$fund_user_id =0;
		if($admin_user_info['admin_type']==ADMIN_TYPE_FUNDUSER){
			$fund_user_id = $admin_user_info['fund_user_id'];
		}
		$id = $_GET['id'];
		if(isset($id)&&$id>0){
			$fund_user_id = $id;
		}
		$url_type = Common::encryptUrl('fund','fd');
		$url_id = Common::encryptUrl($fund_user_id,'fd');
		$url_link = SITE_URL.'funduserInvitation.html?type='.$url_type.'&id='.$url_id;
		
		$data['url_link'] = $url_link;
		return $data;
	}
	
	
	public function userFundLists(){
		$admin_user_info = $_SESSION['admin'];
		$fund_user_id =0;
		if($admin_user_info['admin_type']==ADMIN_TYPE_FUNDUSER){
			$fund_user_id = $admin_user_info['fund_user_id'];
		}
		$id = $_GET['id'];
		if(isset($id)&&$id>0){
			$fund_user_id = $id;
		}
		
		if(!isset($_REQUEST['start_time'])){
			$data['start_time'] = date('Y-m').'-01';
		}else{
			$data['start_time'] = $_GET['start_time'];
		}
		if(!isset($_REQUEST['over_time'])){
			$data['over_time'] = date('Y-m-d');
		}else{
			$data['over_time'] = $_GET['over_time'];
		}
		
		$where = array();
		$where['pay_status'] = PAY_STATUS_2;
		$where['fund_user_flg'] = 1;
		if (!empty($data['start_time'])){
			$where['start_time'] = $data['start_time'];
		}
		if (!empty($data['over_time'])){
			$where['over_time'] =$data['over_time'];
		}
		if (!empty($_GET['serial_number'])){
			$where['serial_number'] =$_GET['serial_number'];
			$data['serial_number'] = $_GET['serial_number'];
		}
		if (!empty($_GET['keyword'])){
			$where['keyword'] =$_GET['keyword'];
			$data['keyword'] = $_GET['keyword'];
		}
		if($fund_user_id>0){
			$where['fund_user_id'] = $fund_user_id;
		}
		$funduserSaleList= HttpServerManager::getFunduserServer()->pagingForsumsale($where);
		$list = array();
		
// 		$fundUserList = Common::array_value_list($funduserSaleList, 'fund_user_id');
// 		$recommendWhere = array();
// 		$recommendWhere['fund_user_id_list'] = implode($fundUserList, ','); 
// 		$recommendUserList = HttpDaoManager::getFunduserDao()->searchList($recommendWhere);
// 		$recommendUserPair = array();
// 		foreach($recommendUserList as $key=>$value){
// 			if(!isset($recommendUserPair[$value['id']])){
// 				$recommendUserPair = array();
// 			}else{
// 				array_push($recommendUserPair[$value['id']],$value['fund_user_id']);
// 			}
// 		} 
		
		$fund_user_rank = Common::_t('fund_user_rank');
		foreach($funduserSaleList['list'] as $key=>$value){
			$where_order = array();
			$where_order['end_time']=$data['start_time'];
			$where_order['pay_status'] = PAY_STATUS_2;
			$where_order['fund_user_id'] = $value['fund_user_id'];
			$userOrderAmount =  HttpDaoManager::getOrderDao()->getSumOrderAmountByfunduser($where_order);
			unset($where_order);
			
			$list[$key]['serial_number']=$value['serial_number'];
			$list[$key]['name']=$value['name'];
			$list[$key]['fund_user_id']=$value['fund_user_id'];
			
			$list[$key]['fund_user_rank_name']=$fund_user_rank[$value['fund_user_rank']];
			$list[$key]['order_sale']=$value['sumamount'];
			$list[$key]['order_sale_fund']=Common::cacluteSummxOrderFund($userOrderAmount+$value['sumamount'],$value['sumamount']);//获取推广数据提成
			
			$where = array();
			$where['recommend_user_id'] = $value['fund_user_id'];
			$where['pay_status'] = PAY_STATUS_2;
			$where['fund_user_flg'] = 1;
			$where['start_time']=$data['start_time'];
			$where['end_time']=$data['over_time'];
			$userRecomendOrderAmount =  HttpServerManager::getFunduserServer()->cacluteSumRecommendOrderFund($where);
			unset($where);
			$list[$key]['recomend_order_amount']=round($userRecomendOrderAmount,2);//获取推荐人提成
			$list[$key]['recomend_order_amount_fund']=$userRecomendOrderAmount*0.05;//获取推广数据提成
		}
		$data['list'] = $list;
		
		return $data;
	
	}


	public function myUserFundLists(){
		$admin_user_info = $_SESSION['admin'];
		$fund_user_id =0;
		if($admin_user_info['admin_type']==ADMIN_TYPE_FUNDUSER){
			$fund_user_id = $admin_user_info['fund_user_id'];
		}
		$id = $_GET['id'];
		if(isset($id)&&$id>0){
			$fund_user_id = $id;
		}
	
		if(!isset($_REQUEST['start_time'])){
			$data['start_time'] = date('Y-m').'-01';
		}else{
			$data['start_time'] = $_GET['start_time'];
		}
		if(!isset($_REQUEST['over_time'])){
			$data['over_time'] = date('Y-m-d');
		}else{
			$data['over_time'] = $_GET['over_time'];
		}
	
		$where = array();
		$where['pay_status'] = PAY_STATUS_2;
		$where['fund_user_flg'] = 1;
		if (!empty($data['start_time'])){
			$where['start_time'] = $data['start_time'];
		}
		if (!empty($data['over_time'])){
			$where['over_time'] =$data['over_time'];
		}
		if (!empty($_GET['serial_number'])){
			$where['serial_number'] =$_GET['serial_number'];
			$data['serial_number'] = $_GET['serial_number'];
		}
		if (!empty($_GET['keyword'])){
			$where['keyword'] =$_GET['keyword'];
			$data['keyword'] = $_GET['keyword'];
		}
		if($fund_user_id>0){
			$where['fund_user_id'] = $fund_user_id;
		}
		$funduserSaleList= HttpServerManager::getFunduserServer()->pagingForsumsale($where);
		$list = array();
	
		// 		$fundUserList = Common::array_value_list($funduserSaleList, 'fund_user_id');
		// 		$recommendWhere = array();
		// 		$recommendWhere['fund_user_id_list'] = implode($fundUserList, ',');
		// 		$recommendUserList = HttpDaoManager::getFunduserDao()->searchList($recommendWhere);
		// 		$recommendUserPair = array();
		// 		foreach($recommendUserList as $key=>$value){
		// 			if(!isset($recommendUserPair[$value['id']])){
		// 				$recommendUserPair = array();
		// 			}else{
		// 				array_push($recommendUserPair[$value['id']],$value['fund_user_id']);
		// 			}
		// 		}
	
		$fund_user_rank = Common::_t('fund_user_rank');
		foreach($funduserSaleList['list'] as $key=>$value){
			$where_order = array();
			$where_order['end_time']=date('Y-m-d',strtotime('-1 day',strtotime($data['start_time'])));
			$where_order['pay_status'] = PAY_STATUS_2;
			$where_order['fund_user_id'] = $fund_user_id;
			$userOrderAmount =  HttpDaoManager::getOrderDao()->getSumOrderAmountByfunduser($where_order);
			unset($where_order);
				
			$list[$key]['serial_number']=$value['serial_number'];
			$list[$key]['name']=$value['name'];
			$list[$key]['fund_user_id']=$value['fund_user_id'];
				
			$list[$key]['fund_user_rank_name']=$fund_user_rank[$value['fund_user_rank']];
			$list[$key]['order_sale']=$value['sumamount'];
			$list[$key]['order_sale_fund']=Common::cacluteSummxOrderFund($userOrderAmount,$value['sumamount']);//获取推广数据提成
				
			$where = array();
			$where['recommend_user_id'] = $value['fund_user_id'];
			$where['pay_status'] = PAY_STATUS_2;
			$where['fund_user_flg'] = 1;
			$where['start_time']=$data['start_time'];
			$where['end_time']=$data['over_time'];
			$userRecomendOrderAmount =  HttpServerManager::getFunduserServer()->cacluteSumRecommendOrderFund($where);
			unset($where);
			$list[$key]['recomend_order_amount']=round($userRecomendOrderAmount,2);//获取推荐人提成
			$list[$key]['recomend_order_amount_fund']=$userRecomendOrderAmount*0.05;//获取推广数据提成
		}
		$data['list'] = $list;
	
		return $data;
	
	}
	
	public function userFundDetailLists(){
		$admin_user_info = $_SESSION['admin'];
		$fund_user_id =0;
		if($admin_user_info['admin_type']==ADMIN_TYPE_FUNDUSER){
			$fund_user_id = $admin_user_info['fund_user_id'];
		}
		$id = $_GET['id'];
		if(isset($id)&&$id>0){
			$fund_user_id = $id;
		}
		
		if(!isset($_REQUEST['start_time'])){
			$data['start_time'] = date('Y-m').'-01';
		}else{
			$data['start_time'] = $_GET['start_time'];
		}
	
		if(isset($_REQUEST['over_time'])){
			$data['over_time'] = $_REQUEST['over_time'];
		}
		$where = array();
		$where['pay_status'] = PAY_STATUS_2;
		$where['fund_user_detail_flg'] = 1;
		if (!empty($data['start_time'])){
			$where['start_time'] = $data['start_time'];
		}
		if (!empty($data['over_time'])){
			$where['over_time'] =$data['over_time'];
		}
		if (!empty($_GET['keyword'])){
			$where['keyword'] =$_GET['keyword'];
			$data['keyword'] = $_GET['keyword'];
		}
		
		if($fund_user_id>0){
			$where['fund_user_id'] = $fund_user_id;
		}
		/**
		 **DETAIL_DATA	
		 */
		$funduserSaleList= HttpServerManager::getFunduserServer()->pagingFordetailsale($where);
		$list = array();
		foreach($funduserSaleList['list'] as $key=>$value){
			$where_order = array();
			$where_order['end_time']=date('Y-m-d H:i:s',$value['add_time']);
			$where_order['pay_status'] = PAY_STATUS_2;
			$where_order['fund_user_id'] = $fund_user_id;			
			$userOrderAmount =  HttpDaoManager::getOrderDao()->getSumOrderAmountByfunduser($where_order);

			$list[$key]['order_no']=$value['order_no'];
			$list[$key]['english_name']=$value['english_name'];
			$list[$key]['phone']=$value['phone'];
			$list[$key]['email']=$value['email'];
			$list[$key]['order_sale']=$value['sumamount'];
			$list[$key]['order_sale_fund']=Common::cacluteSummxOrderFund($userOrderAmount,$value['sumamount']);
		}
		$data['list'] = $list;
		$data['page'] = $funduserSaleList['page'];
		return $data;
	
	}


	public function userFundRecommendDetailLists(){
		$admin_user_info = $_SESSION['admin'];
		$fund_user_id =0;
		if($admin_user_info['admin_type']==ADMIN_TYPE_FUNDUSER){
			$fund_user_id = $admin_user_info['fund_user_id'];
		}
		$id = $_GET['id'];
		if(isset($id)&&$id>0){
			$fund_user_id = $id;
		}
	
		if(!isset($_REQUEST['start_time'])){
			$data['start_time'] = date('Y-m').'-01';
		}else{
			$data['start_time'] = $_GET['start_time'];
		}
	
		if(isset($_REQUEST['over_time'])){
			$data['over_time'] = $_REQUEST['over_time'];
		}
		$where = array();
		$where['pay_status'] = PAY_STATUS_2;
		$where['fund_user_detail_flg'] = 1;
		if (!empty($data['start_time'])){
			$where['start_time'] = $data['start_time'];
		}
		if (!empty($data['over_time'])){
			$where['over_time'] =$data['over_time'];
		}
		if (!empty($_GET['keyword'])){
			$where['keyword'] =$_GET['keyword'];
			$data['keyword'] = $_GET['keyword'];
		}
		
		if($fund_user_id>0){
			$where['recommend_user_id'] = $fund_user_id;
		}
		/**
		 **DETAIL_DATA
		 */
		$userRecommendSaleList= HttpServerManager::getFunduserServer()->pagingForRecommenddetailsale($where);
		$id_list = Common::array_value_list($userRecommendSaleList, 'fund_user_id');
		$where['id_list_str'] =implode($id_list,',');
		$fund_user_id_list_str = HttpDaoManager::getFunduserDao()->searchList($where);
		$fund_user_pair = array();
		foreach($fund_user_id_list_str as $key=>$value){
			$fund_user_pair[$value['id']]['name']=$value['name'];
			$fund_user_pair[$value['id']]['serial_number']=$value['serial_number'];
		}
		$list = array();
		foreach($userRecommendSaleList['list'] as $key=>$value){
			$list[$key]['serial_number'] = $fund_user_pair[$value['fund_user_id']]['serial_number'];
			$list[$key]['name'] = $fund_user_pair[$value['fund_user_id']]['name'];
			$list[$key]['recommend_order_sale']=$value['sumamount'];
			$list[$key]['recommend_order_sale_fund']=$value['sumamount']*0.05;
		}
		$data['list'] = $list;
		$data['page'] = $funduserSaleList['page'];
		return $data;
	
	}
	
	
	public function add(){
		$data = array();
		if (!empty($_GET['id'])){
			$data = HttpDaoManager::getFunduserDao()->get($_GET['id']);
		}else{
			$sysKey = 'FUND_NO';
			$key_info  =  HttpDaoManager::getSysConfigDao()->getBySyskey($sysKey);
			$seed_no = $key_info['sys_value'];
			$data['serial_number'] = Common::executeSerialNumber('F','9',$seed_no);
			$sys_update = array();
			$sys_update['sys_value']=$seed_no+1;
			$sys_id = $key_info['id'];
			$key_info  =  HttpDaoManager::getSysConfigDao()->update($sys_update,$sys_id);			
		}
		$sex  = Common::_t('sex');
		$job_type = Common::_t('fund_user_job');
		
		$data['sex'] = isset($data['sex'])?$data['sex']:'';
		$data['sexOptions'] = page_modifier_options($sex,$data['sex']);

		$data['status'] = isset($data['status'])?$data['status']:0;
		
		$status = Common::_t('fund_user_status');
		$data['statusOptions'] = page_modifier_options($status,$data['status']);
		
		$fund_user_rank = Common::_t('fund_user_rank');
		//$data['userTypeOptions'] = page_modifier_options($user_type,$data['user_type']);
	
		
		$province = Common::getStrValue($data,'province');
		$city =Common::getStrValue($data,'city');
		$data['select'] = HttpServerManager::getRegionServer()->cityProvinceSelect($province, $city,'province','city');
		$employees = HttpDaoManager::getEmployeesDao()->getListByDepartment(Common::_t('faculty_member'));
		$tmps=array();
		foreach ($employees as $v){
			$tmps[$v['id']] = $v['name'];
		}
		
		$data['id'] = Common::getGetStrValue('id');
		
	
		$funduser_pair = array();
		$funduser_list =  HttpDaoManager::getFunduserDao()->searchList();
		foreach($funduser_list as $key=>$value){
			if($value['id']==$data['id']){
				continue;			
			}

			if($value['fund_user_id']==$data['id']){//他邀请的人，不能选择他
				continue;
			}
			
			$funduser_pair[$value['id']] = $value['serial_number'].'-'.$value['name'];
		}		
		
		$data['funduserOptions'] = page_modifier_options_ext($funduser_pair,common::getStrValue($data, 'fund_user_id'));
		$data['trackPeopleSelect'] =   page_modifier_options($tmps,common::getStrValue($data, 'employee_id'));
		$data['userJobOptions'] =   page_modifier_options($job_type,common::getStrValue($data, 'job'));
		return $data;
	}
	
	public function subAdd(){
		$tmp = HttpServerManager::getFunduserServer()->add($_POST);
		if($tmp===TRUE){
			$msg = Common::_t('data_insert_success');
			ClassManager::getToos()->msgbox($msg);
		}else{
			$error = Common::_t('eror_registerbox');
			ClassManager::getToos()->msgbox($error[$tmp]);			
		}
	}
	
	public function subUpdate(){
		HttpServerManager::getFunduserServer()->update($_POST, $_POST['id']);
		ClassManager::getToos()->msgbox(Common::_t('data_edit_success'),'funduser_lists.html');
	}
	public function delete(){
		HttpServerManager::getTeacherServer()->deleteBatchByid($_GET['id']);
		ClassManager::getToos()->msgbox(Common::_t('data_del_success'));
	}
}
?>